package com.mj.guoqi.activity;

import com.SYY.cbsdk.ui.AppControl;
import com.dyk.hfsdk.dao.util.DevListener;
import com.dyk.hfsdk.ui.Access;
import com.mj.guoqi.R;
import com.mj.guoqi.bean.GuoqiDto;
import com.mj.guoqi.util.AppUtils;
import com.mj.guoqi.util.HttpUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
/**
 * 国旗详情
 * @author zhaominglei
 * @date 2015-8-15
 */
public class GuoqiDetailActivity extends Activity implements OnClickListener,DevListener {
	@SuppressWarnings("unused")
	private static final String TAG = GuoqiDetailActivity.class.getSimpleName();
	private ImageView goHome;
	private LinearLayout miniAdLinearLayout; //迷你广告
	private TextView nameText; //名称
	private String name; //国家名称
	private ImageView logoView; //国旗
	private TextView introductionText; //介绍
	private GuoqiDto guoqiDto;
	private Button appOffersButton; //推荐应用
	private Access access;
	private AppControl appControl;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_guoqi_detail);
		init();
	}
	
	private void init() {
		goHome = (ImageView)findViewById(R.id.guoqidetail_gohome);
		miniAdLinearLayout = (LinearLayout)findViewById(R.id.guoqidetail_miniAdLinearLayout);
		nameText = (TextView)findViewById(R.id.guoqidetail_name);
		logoView = (ImageView)findViewById(R.id.guoqidetail_logo);
		introductionText = (TextView)findViewById(R.id.guoqidetail_introduction);
		appOffersButton = (Button)findViewById(R.id.guoqidetail_appOffersButton);
		
		goHome.setOnClickListener(this);
		introductionText.setOnClickListener(this);
		appOffersButton.setOnClickListener(this);
		
		appControl = AppControl.getInstance();
		appControl.init(GuoqiDetailActivity.this, MainActivity.MUMAYI_APPID, MainActivity.MUMAYI_APPCHANNEL);
		appControl.loadPopAd(GuoqiDetailActivity.this);
		appControl.showPopAd(GuoqiDetailActivity.this, 60 * 1000);
		appControl.showInter(GuoqiDetailActivity.this, miniAdLinearLayout);
		
		access = Access.getInstance();
		// 初始化带player_id
		access.init(GuoqiDetailActivity.this, MainActivity.MUMAYI_APPID, MainActivity.MUMAYI_APPCHANNEL);
		// 设置初始积分                                                              
		access.setdefaultSCORE(this, 100);
		// 设置获取积分的监听
		access.setAppListener(this, this);
		
		guoqiDto = (GuoqiDto)getIntent().getSerializableExtra("guoqiDto");
		if (guoqiDto == null) {
			Toast.makeText(getApplicationContext(), R.string.guoqi_error2, Toast.LENGTH_SHORT).show();
			GuoqiDetailActivity.this.finish();
		}
		
		name = guoqiDto.getName();
		nameText.setText(name);
		String imageName = guoqiDto.getLogo().replaceAll("img/", "").replaceAll(".gif", "");
		logoView.setImageResource(AppUtils.getResId(getApplicationContext(), imageName, "drawable"));
		introductionText.setText(guoqiDto.getIntroduction()+"   点击查看百度百科");
	}
	
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.guoqidetail_gohome:
			GuoqiDetailActivity.this.finish();
			break;
			
		case R.id.guoqidetail_introduction:
			if (name != null && !name.equals("")) {
				String keyword = name + "国旗";
				String baikeURL = "http://baike.baidu.com/search/word?word="+HttpUtils.encodeURI(keyword);
				Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.setData(Uri.parse(baikeURL));
				startActivity(intent);
			}
			break;
			
		case R.id.guoqidetail_appOffersButton:
			access.openWALL(GuoqiDetailActivity.this);
			break;
		default:
			break;
		}
	}
	
	@Override
	public void onBackPressed() {
		GuoqiDetailActivity.this.finish();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		appControl.close(this);
	}
	
	@Override
	public void onDevSucceed(int eggs) {
		System.out.println("积分获取成功:" + eggs);
	}

	@Override
	public void onDevFailed(String message) {
		System.out.println("积分获取失败:" + message);
	}

	@Override
	public void onClose(Context context) {
		((Activity) context).finish();
	}		
}
